/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.dataprocessing.service.impl;

import com.icinfo.cloud.provider.punish.dataprocessing.mapper.DataProcessFieldConfigInfoMapper;
import com.icinfo.cloud.provider.punish.dataprocessing.model.DataProcessFieldConfigInfo;
import com.icinfo.cloud.provider.punish.dataprocessing.service.IDataProcessFieldConfigInfoService;
import com.icinfo.cloud.provider.punish.dataprocessing.vo.DataProcessCaseFieldListVo;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述: 工单处理-字段配置信息 data_process_field_config_info 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2023年02月03日
 */
@Service
public class DataProcessFieldConfigInfoServiceImpl extends MyBatisServiceSupport implements IDataProcessFieldConfigInfoService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(DataProcessFieldConfigInfoServiceImpl.class);

    @Autowired
    private DataProcessFieldConfigInfoMapper dataProcessFieldConfigInfoMapper;

    /**
     * 获取字段列表
     *
     * @param type 类型（1：案件，2：案源）
     * @return 字段列表
     * @author lilihao
     * @date 2023/2/7 20:25
     */
    @Override
    public List<DataProcessFieldConfigInfo> getListByType(String type) {
        Example example = new Example(DataProcessFieldConfigInfo.class);
        example.createCriteria().andEqualTo("type", type);
        example.orderBy("sort").asc();
        return dataProcessFieldConfigInfoMapper.selectByExample(example);
    }

    /**
     * 校验是否更新字段一致性
     *
     * @param fieldListVoList 字段列表
     * @return true：一致，false：不一致
     * @author lilihao
     * @date 2023/2/7 21:43
     */
    @Override
    public boolean checkConsistency(List<DataProcessCaseFieldListVo> fieldListVoList) {
        List<String> fieldList = fieldListVoList.stream().map(DataProcessCaseFieldListVo::getFieldName).collect(Collectors.toList());
        List<String> tableList = fieldListVoList.stream().map(DataProcessCaseFieldListVo::getTableName).collect(Collectors.toList());
        Example example = new Example(DataProcessFieldConfigInfo.class);
        example.createCriteria().andIn("tableName", tableList)
                .andIn("fieldName", fieldList);
        int count = dataProcessFieldConfigInfoMapper.selectCountByExample(example);

        return count == fieldList.size();
    }
}